<template>
  <!-- props--{{ props }} -->
  <div v-for="(childChat, childIndex) in list" @click="clickChild(childIndex)">
    <!-- childchat--{{ childChat }} -->
    <chat-content :content="childChat.answer || childChat.content" :isAi="true"></chat-content>
    <div class="operate-list flex" v-show="index == (chatStore.chatList.length - 1) || (clickIndex === childIndex)">
      <van-button @click="refreshAiChild(childChat, childIndex)" size="small" round class="van-button">
        <div class="operate-btn flex"> <i class="ai-icon-refresh"></i>刷新</div>
      </van-button>
      <van-button @click="copyText(childChat.answer || childChat.content)" size="small" round>
        <div class="operate-btn flex"> <i class="ai-icon-copy"></i>复制</div>
      </van-button>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, inject, defineProps, computed, onMounted } from 'vue';
import ChatContent from './ChatContent.vue'
import { useChatStore } from '@/store';
import Ai from './AI';
const chatStore = useChatStore();
const clickIndex = ref('');
// onMounted(() => {
//   ;
//   console.log("ai-onMounted");
// });
const props = defineProps({
  list: Array,
  question: {
    type: String
  },
  index: Number
});
/**
 * @description 复制文本
 * @param {String} text 要复制的文本内容
 */
function copyText(text) {
  wx.setClipboardData({
    data: text,
    success(res) {
      wx.getClipboardData({
        success(res) {
        }
      })
    }
  })
}
function clickChild(index) {
  if (clickIndex.value == '') {
    clickIndex.value = index;
  } else {
    clickIndex.value = '';
  }
}
function refreshAiChild(childItem, childIndex) {
  chatStore.resetConversation();
  let content = childItem.answer || childItem.content;
  let ai = new Ai({
    apiType: "stream",
    answerType: 'ai',
    question: `生成1条类似的文案:${content}，文案字数不高于${50}个字.`,
    extraData: {
      index: props.index,
      childIndex
    }
  });
  ai.getAiAnserFn();

}
</script>

<style lang="scss" scoped>
.operate-list {
  i {
    display: inline-block;
    margin-right: 20px;
  }

  .van-button {
    margin-right: 24rpx;
  }
}
</style>